var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    questionArray:[],
    pageNo:1,
    page:1
  },
  toContent:function(e){
    console.log(e)
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../onlineContent/onlineContent?id='+id,

    })
  },
  toAsk:function(event){
    wx.navigateTo({
      url: '../askPage/askPage',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'onlinequestion/question',
      data: {

      },
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        var resData = res.data.data.data
        for (var i = 0; i < res.data.data.data.length; i++) {
          var title = 'questionArray[' + i + '].title'
          var time = 'questionArray[' + i + '].time'
          var avatar = 'questionArray[' + i + '].avatar'
          var imgDis = 'questionArray[' + i + '].imgDis'
          var commits = 'questionArray[' + i + '].commits'
          var imgArray = 'questionArray[' + i + '].imgArray'
          var nickName = 'questionArray[' + i + '].nickName'
          var id = 'questionArray[' + i + '].id'
          var imgBool
          var imageArray
          if (resData[i].imgList.length != 0) {
            imgBool = true

          } else {
            imgBool = false
          }
          // var imageArr=[]
          // if(resData[i].imgList.length>0){
          //   for (var j = 0; j < resData[i].imgList.length; j++) {
          //     imageArr.push(resData[i].imgList[j].url)
          //   }
          // } 
          
          that.setData({
            [title]: resData[i].content,
            [time]: resData[i].gmtCreateFormat.substring(0, 10) + ' ' + resData[i].gmtCreateFormat.substring(11, 13),
            [avatar]: resData[i].user.avatarUrl,
            [imgDis]: imgBool,
            [commits]: resData[i].commentList.length,
            [imgArray]: resData[i].imgList,
            [nickName]: resData[i].user.nickName,
            [id]: resData[i].id
          })


        }

      }
    })
  }, 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getMoreQuestion:function(page){
    var that=this
     wx.request({
       url: app.globalData.baseUrl + 'onlinequestion/question',
       data: {
          pageNo:page,
          pageSize:10
       },
       header: {
         'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
       },
       method: 'GET',
       success:function(res){
         console.log(res)
        
          if (res.data.data.data > 0 && res.data.data.data.length < 10) {
            var resData = res.data.data.data
            for (var i = (page-1)* 10; i < res.data.data.data.length + (page-1) * 10; i++) {
              var title = 'questionArray[' + i + '].title'
              var time = 'questionArray[' + i + '].time'
              var avatar = 'questionArray[' + i + '].avatar'
              var imgDis = 'questionArray[' + i + '].imgDis'
              var commits = 'questionArray[' + i + '].commits'
              var imgArray = 'questionArray[' + i + '].imgArray'
              var nickName = 'questionArray[' + i + '].nickName'
              var id = 'questionArray[' + i + '].id'
              var imgBool
              var imageArray
              if (resData[i % 10].imgList.length != 0) {
                imgBool = true

              } else {
                imgBool = false
              }
              that.setData({
                [title]: resData[i % 10].content,
                [time]: resData[i % 10].gmtCreateFormat.substring(0, 10) + ' ' + resData[i % 10].gmtCreateFormat.substring(11, 13),
                [avatar]: resData[i % 10].user.avatarUrl,
                [imgDis]: imgBool,
                [commits]: resData[i % 10].commentList.length,
                [imgArray]: resData[i % 10].imgList,
                [nickName]: resData[i % 10].user.nickName,
                [id]: resData[i % 10].id
              })


            }

          } else {
            var resData = res.data.data.data
            for (var i = (page-1) * 10; i < page * 10; i++) {
              var title = 'questionArray[' + i + '].title'
              var time = 'questionArray[' + i + '].time'
              var avatar = 'questionArray[' + i + '].avatar'
              var imgDis = 'questionArray[' + i + '].imgDis'
              var commits = 'questionArray[' + i + '].commits'
              var imgArray = 'questionArray[' + i + '].imgArray'
              var nickName = 'questionArray[' + i + '].nickName'
              var id = 'questionArray[' + i + '].id'
              var imgBool
              var imageArray
              try{
                if (resData[i % 10].imgList.length != 0) {
                  imgBool = true

                } else {
                  imgBool = false
                }
              }catch(err){

              }
              
              that.setData({
                [title]: resData[i % 10].content,
                [time]: resData[i % 10].gmtCreateFormat.substring(0, 10) + ' ' + resData[i % 10].gmtCreateFormat.substring(11, 13),
                [avatar]: resData[i % 10].user.avatarUrl,
                [imgDis]: imgBool,
                [commits]: resData[i % 10].commentList.length,
                [imgArray]: resData[i % 10].imgList,
                [nickName]: resData[i % 10].user.nickName,
                [id]: resData[i % 10].id
              })


            }
        }
         
          

          
       }
     })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     var p=this.data.pageNo+1
     this.setData({
       pageNo:p
     })
     this.getMoreQuestion(p)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})